<?php
$file = fopen("C:/Documents and Settings/F3557790/Desktop/Temp/lotto.csv", "r") or exit("Unable to open file!");
$dateFound = false;
$machineAArray = array(49);
$machineBArray = array(49);
$machineCArray = array(49);
$machines = array(3);
$machines["Mandlakazi"] = 0;
$machines["Dimakatso"] = 0;
$machines["Mooinooi"] = 0;

while(!feof($file)){

	$aline = fgets($file);
	if ($aline ==  ""){
		continue;
	}

	if ($dateFound){
		$line =  split(",", $aline);

		$machine = $line[29];
		$arrayToUse = null;

		if ($machine == "Mandlakazi"){
			$machines["Mandlakazi"] = $machines["Mandlakazi"]+1;

			$ballVal = (int)$line[2];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			$ballVal = (int)$line[3];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			$ballVal = (int)$line[4];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			$ballVal = (int)$line[5];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			$ballVal = (int)$line[6];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			$ballVal = (int)$line[7];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}

			//Bonus Ball
			$ballVal = (int)$line[8];
			if (!isset($machineAArray[$ballVal])) {
				$machineAArray[$ballVal] = 1;
			} else {
				$machineAArray[$ballVal] = ($machineAArray[$ballVal]+1);
			}
		} else if ($machine == "Dimakatso") {
			$machines["Dimakatso"] = $machines["Dimakatso"]+1;
				
			$ballVal = (int)$line[2];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			$ballVal = (int)$line[3];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			$ballVal = (int)$line[4];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			$ballVal = (int)$line[5];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			$ballVal = (int)$line[6];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			$ballVal = (int)$line[7];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}

			//Bonus Ball
			$ballVal = (int)$line[8];
			if (!isset($machineBArray[$ballVal])) {
				$machineBArray[$ballVal] = 1;
			} else {
				$machineBArray[$ballVal] = ($machineBArray[$ballVal]+1);
			}
		} else if ($machine == "Mooinooi") {
			$machines["Mooinooi"] = $machines["Mooinooi"]+1;
				
			$ballVal = (int)$line[2];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			$ballVal = (int)$line[3];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			$ballVal = (int)$line[4];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			$ballVal = (int)$line[5];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			$ballVal = (int)$line[6];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			$ballVal = (int)$line[7];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}

			//Bonus Ball
			$ballVal = (int)$line[8];
			if (!isset($machineCArray[$ballVal])) {
				$machineCArray[$ballVal] = 1;
			} else {
				$machineCArray[$ballVal] = ($machineCArray[$ballVal]+1);
			}
		}


	}



	if (strlen(strstr($aline,"Draw No"))>0) {
		$dateFound = true;
	}

}

asort($machineAArray);
asort($machineBArray);
asort($machineCArray);
asort($machines);

echo "MACHINES USAGE <br/>";
foreach($machines as $key => $value){
	echo $key."  ==>  ".$value. "<br/>";
}
echo "==================================================== <br/>";

echo "Mandlakazi <br/>";
foreach($machineAArray as $key => $value){
	if ($key != 0)
	echo $key."  ==>  ".$value. "<br/>";
}
echo "==================================================== <br/>";

echo "Dimakatso <br/>";
foreach($machineBArray as $key => $value){
	if ($key != 0)
	echo $key."  ==>  ".$value. "<br/>";
}
echo "==================================================== <br/>";

echo "Mooinooi <br/>";
foreach($machineCArray as $key => $value){
	if ($key != 0)
	echo $key."  ==>  ".$value. "<br/>";
}
echo "==================================================== <br/>";

?>